import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
import 'package:flutter_bsbdj/bean/music_persional.dart';
import 'package:flutter_bsbdj/pagers/music/music_play_detail.dart';
import 'package:flutter_bsbdj/utils/shared_preferences_util.dart';
import 'package:flutter_bsbdj/utils/widget_views.dart';
import 'package:flutter_bsbdj/utils/music_play.dart';
import 'package:flutter_bsbdj/pagers/music/music_suspension_window.dart';
import 'package:flutter_bsbdj/utils/toast.dart';

////音乐人频道
class ChannelManDetail extends StatefulWidget {
  String name;
  String title;

  ChannelManDetail(this.title, this.name);

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return DetailWidget();
  }
}

class DetailWidget extends State<ChannelManDetail> {
  String url_cid_3 = "https://api.apiopen.top/searchMusic?name=";

  bool loading = true;

  List<PersonalItem> _musicItems;

  double _marginBottom = 0;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _getChannelDetail();
    _changMarginBottom();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: loading
          ? WidgetViews.getInstance().loading()
          : Container(
              child: (_musicItems == null || _musicItems.length == 0)
                  ? Container(
                      child: WidgetViews.getInstance().nullDataPormat(() {
                        _getChannelDetail();
                      }),
                    )
                  : Container(
                      margin: EdgeInsets.only(bottom: _marginBottom),
                      child: ListView.builder(
                          itemCount: _musicItems.length,
                          itemBuilder: (content, index) {
                            PersonalItem item = _musicItems[index];
                            return item.title == null
                                ? Container()
                                : GestureDetector(
                                    child: Container(
                                      height: 70,
                                      color: Colors.white,
                                      margin: EdgeInsets.all(5),
                                      padding: EdgeInsets.all(5),
                                      child: Row(
                                        children: <Widget>[
                                          Container(
                                            width: 60,
                                            height: 60,
                                            margin: EdgeInsets.only(right: 8),
                                            child: WidgetViews.getInstance()
                                                .cachedImageCover(item.pic),
                                          ),
                                          Expanded(
                                            child: Column(
                                              crossAxisAlignment:
                                                  CrossAxisAlignment.start,
                                              children: <Widget>[
                                                Text(
                                                  item.title,
                                                  style: TextStyle(
                                                    fontSize: 15,
                                                    color: Colors.blueAccent,
                                                  ),
                                                ),
                                                Expanded(
                                                  child: Container(
                                                    alignment:
                                                        Alignment.bottomLeft,
                                                    child: Text(
                                                      item.author,
                                                      style: TextStyle(
                                                          color: Colors.grey),
                                                    ),
                                                  ),
                                                )
                                              ],
                                            ),
                                          )
                                        ],
                                      ),
                                    ),
                                    onTap: () {
                                      MusicWindowPlay play =
                                          MusicWindowPlay.getInstance(context);
                                      play.playMusic(item.url, item, context);
                                      if (play.musicIsPlayShow()) {
                                        play.isHeidden(true);
                                      }
                                      print(item.toString());
                                      Navigator.of(context)
                                          .push(MaterialPageRoute(
                                              builder: (context) =>
                                                  MusicDetail(item)))
                                          .then((value) {
                                        setState(() {
                                          if (play.musicIsPlaying()) {
                                            play.isHeidden(false);
                                            _marginBottom = 60;
                                          } else {
                                            _marginBottom = 0;
                                          }
                                        });
                                      });
                                    },
                                  );
                          }),
                    ),
            ),
    );
  }

  void _getChannelDetail() async {
    var t = await Dio().get("$url_cid_3${widget.name}").then((value) {
      try {
        MusicMan man = MusicMan.fromJson(value.data);
        //只有当前widget还在 tree中才设置值
        if (mounted) {
          setState(() {
            _musicItems = man.result;
            loading = false;
          });
        }
      } catch (e) {}
    });
  }

  void _changMarginBottom() async {
    await Future.delayed(Duration(milliseconds: 400), () {
      setState(() {
        if (MusicWindowPlay.getInstance(context).musicIsPlaying()) {
          _marginBottom = 60;
          MusicWindowPlay.getInstance(context).isHeidden(false);
        } else {
          _marginBottom = 0;
        }
      });
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }
}
